package com.ruoyi.system.controller;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.po.ExamPo;
import com.ruoyi.system.domain.vo.Score;
import com.ruoyi.system.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考试表Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/exam")
public class ExamController extends BaseController
{
    @Autowired
    private IExamService examService;

    @Autowired
    private IExamStudentService examStudentService;

    @Autowired
    private ISubjectService subjectService;

    @Autowired
    private IPaperService paperService;

    @Autowired
    private IPaperSubjectService paperSubjectService;

    @Autowired
    private IStudentSubjectService studentSubjectService;

    @PostMapping("/jisuan")
    public AjaxResult jisuanchengji(@RequestBody Score score){
        int right=0;
        Long studentId = score.getStudentId();
        Long examId = score.getExamId();
        StudentSubject studentSubject=new StudentSubject();
        studentSubject.setStudentId(studentId);
        studentSubject.setExamId(examId);
        List<StudentSubject> choose=studentSubjectService.selectStudentSubjectList(studentSubject);
        Exam exam = examService.selectExamByExamId(examId);
        Long paperId = exam.getPaperId();
        List<PaperSubject> subjectList = paperSubjectService.selectPaperSubjectByPaperId(paperId);
        for (int i=0;i<subjectList.size();i++){
            Long subjectId = subjectList.get(i).getSubjectId();
            Subject subject = subjectService.selectSubjectBySubjectId(subjectId);
            String rightAnswer = subject.getRightAnswer();
            for(int j=0;j<choose.size();j++){
                if(choose.get(j).getSubjectId().equals(subjectId)){
                    if(rightAnswer.equals(choose.get(j).getAnswer())){
                        right++;
                    }
                }
            }
        }
        DecimalFormat df=new DecimalFormat("0.00");//设置保留位数
        double s = Double.parseDouble(df.format((float) right / subjectList.size()));
        Date date=new Date();
        ExamStudent examStudent=new ExamStudent();
        examStudent.setExamId(score.getExamId());
        examStudent.setStudentId(score.getStudentId());
        List<ExamStudent> examStudentList = examStudentService.selectExamStudentList(examStudent);
        if (examStudentList.size()==0){
            examStudent.setScore((long) (s*100));
            examStudent.setIsOk("ok");
            examStudent.setSubmitTime(date);
            examStudentService.insertExamStudent(examStudent);
        }else {
            examStudent.setScore((long) (s*100));
            examStudent.setIsOk("ok");
            examStudent.setSubmitTime(date);
            examStudentService.updateExamStudent(examStudent);
        }



        examStudentService.updateExamStudent(examStudent);
        return success(s*100);
    }



    /**
     * 查询考试表列表
     */
    @PreAuthorize("@ss.hasPermi('system:exam:list')")
    @GetMapping("/list")
    public TableDataInfo list(Exam exam)
    {
        startPage();
        List<ExamPo> examList=new ArrayList<>();
        List<Exam> list = examService.selectExamList(exam);
        for (int i=0;i<list.size();i++){
            Paper paper = paperService.selectPaperByPaperId(list.get(i).getPaperId());
            String name = paper.getName();
            Long count = paper.getCount();
            String producer = paper.getProducer();
            ExamPo examPo=new ExamPo(list.get(i),name,count,producer);
            examList.add(examPo);
        }
        return getDataTable(examList);
    }

    /**
     * 导出考试表列表
     */
    @PreAuthorize("@ss.hasPermi('system:exam:export')")
    @Log(title = "考试表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Exam exam)
    {
        List<Exam> list = examService.selectExamList(exam);
        ExcelUtil<Exam> util = new ExcelUtil<Exam>(Exam.class);
        util.exportExcel(response, list, "考试表数据");
    }

    /**
     * 获取考试表详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:exam:query')")
    @GetMapping(value = "/{examId}")
    public AjaxResult getInfo(@PathVariable("examId") Long examId)
    {
        return success(examService.selectExamByExamId(examId));
    }

    /**
     * 新增考试表
     */
    @PreAuthorize("@ss.hasPermi('system:exam:add')")
    @Log(title = "考试表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Exam exam)
    {
        return toAjax(examService.insertExam(exam));
    }

    /**
     * 修改考试表
     */
    @PreAuthorize("@ss.hasPermi('system:exam:edit')")
    @Log(title = "考试表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Exam exam)
    {
        return toAjax(examService.updateExam(exam));
    }

    /**
     * 删除考试表
     */
    @PreAuthorize("@ss.hasPermi('system:exam:remove')")
    @Log(title = "考试表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{examIds}")
    public AjaxResult remove(@PathVariable Long[] examIds)
    {
        return toAjax(examService.deleteExamByExamIds(examIds));
    }
}
